<?php
class UserAction extends CommonAction {
	public function _before_add() {
		$model = M ( "Role" );
		$list = $model->select ();
		$this->assign ( 'list', $list );
	}
	public function _before_edit() {
		$model = M ( "Role" );
		$list = $model->select ();
		$this->assign ( 'list', $list );
	}
	public function index() {
		/**
		 * 分页
		 */
		import ( '@.Org.Page' );
		$User = M ( 'User' );
		$map = array ();
		
		if ($_REQUEST ['username1']) {
			//dump($_REQUEST ['username1']);
			$keyword = $_REQUEST ['username1'];
			$map ['username'] = array ('like','%' . $keyword . '%' );
			$this->assign ( 'username1', $keyword );
		}
		$map['id'] = array('neq',1);//不显示id=1的admin的管理员
		$count = $User->where ( $map )->count ();
		$page = new Page ( $count, ! empty ( $_REQUEST ['numPerPage'] ) ? $_REQUEST ['numPerPage'] : 20 );
		$pageNum = ! empty ( $_REQUEST ['pageNum'] ) ? $_REQUEST ['pageNum'] : 1;
		$list = $User->page ( $pageNum . "," . $page->listRows )->where ( $map )->select ();
		$this->assign ( 'list', $list );
		$this->assign ( 'totalCount', $count ); // 总条数
		$this->assign ( 'numPerPage', $page->listRows ); // 每页显示的条数
		$this->assign ( 'currentPage', $pageNum ); // 当前页
		$this->display ();
	}
	// 添加
	public function insert() {
		/**
		 * 个人资料照片上传
		 */
		if (empty ( $_FILES )) {
			$this->error ( '必须选择上传照片' );
		} else {
			import ( "@.Org.UploadFile" );
			$upload = new UploadFile ();
			$upload->maxSize = 3145728;
			$upload->thumbMaxWidth = '300,500';
			$upload->thumbMaxHeight = '200,400';
			$upload->allowExts = array (
					'jpg',
					'gif',
					'png',
					'jpeg' 
			);
			$upload->savePath = "./Public/Uploads/userdata/";
			$upload->saveRule = "time";
			if (! $upload->upload ()) {
			} else {
				// 上传成功，获取上传信息
				$info = $upload->getUploadFileInfo ();
			}
		}
		$User = D ( 'User' );
		$userdata = $User->getByUsername ( $_POST ['username'] );
		/**
		 * 判断账户是否已存在
		 */
		if ($userdata) {
			$this->ajaxReturn ( '0', '该 登录账号 已经存在!', '300' );
		} else {
			if (empty ( $info )) {
				if (empty ( $_POST ['photo'] )) {
					$imgurl = "/Uploads/userdata/default.png";
				} else {
					$imgurl = $_POST ['photo'];
				}
			} else {
				$savename = $info [0] ['savename'];
				$imgurl = "/Uploads/userdata/" . $savename;
			}
			if ($User->create ()) {
				$data ['username'] = $_POST ['username'];
				$data ['password'] = md5 ( $_POST ['password'] );
				$data ['fullname'] = $_POST ['fullname'];
				$data ['sex'] = $_POST ['sex'];
				$data ['birthday'] = $_POST ['birthday'];
				$data ['email'] = $_POST ['email'];
				$data ['depid'] = $_POST ['depid'];
				$data ['jobid'] = $_POST ['jobid'];
				$data ['phone'] = $_POST ['phone'];
				$data ['fax'] = $_POST ['fax'];
				$data ['address'] = $_POST ['address'];
				$data ['accessiontime'] = $_POST ['accessiontime'];
				$data ['education'] = $_POST ['education'];
				$data ['borrowamount'] = $_POST ['borrowamount'];
				$data ['createtime'] = time ();
				$data ['status'] = $_POST ['status'];
				$data ['photo'] = $imgurl;
				
				$result = $User->add ( $data );
				if ($result) {
					$this->ajaxReturn ( $result, "信息添加成功", '200' );
				} else {
					$this->ajaxReturn ( 0, '信息添加失败！', 300 );
				}
			} else {
				$this->error ( $User->getError () );
			}
		}
	}
	
	// 更新
	public function update() {
		/**
		 * 个人资料照片修改
		 */
		if (empty ( $_FILES )) {
			$this->error ( '必须选择上传照片' );
		} else {
			import ( "@.Org.UploadFile" );
			$upload = new UploadFile ();
			$upload->maxSize = 3145728;
			$upload->thumbMaxWidth = '300,500';
			$upload->thumbMaxHeight = '200,200';
			$upload->allowExts = array (
					'jpg',
					'gif',
					'png',
					'jpeg' 
			); // 上传照片格式
			$upload->savePath = "./Public/Uploads/userdata/";
			$upload->saveRule = "time";
			if (! $upload->upload ()) {
			} else {
				// 上传成功，获取上传信息
				$info = $upload->getUploadFileInfo ();
			}
		}
		$User = D ( 'User' );
		$id=$_REQUEST['id'];
		$userdata = $User->getByUsername ( $_POST ['username'] );
		/**
		 * 判断账户是否已存在
 		*/
		if($userdata&&$userdata['id']!==$id){
			
				$this->ajaxReturn ( '0', '该 登录账号 已经存在!', '300' );
			
		}else {
		if ($User->create ()) {
			if (empty ( $info )) {
				if (empty ( $_POST ['photo'] )) {
					$imgurl = "/Uploads/userdata/default.png";
				} else {
					$imgurl = $_POST ['photo'];
				}
			} else {
				$savename = $info [0] ['savename'];
				$imgurl = "/Uploads/userdata/" . $savename;
			}
			$id = $_POST ['id'];
			$data ['username'] = $_POST ['username'];
			
			$data ['fullname'] = $_POST ['fullname'];
			$data ['sex'] = $_POST ['sex'];
			$data ['birthday'] = $_POST ['birthday'];
			$data ['email'] = $_POST ['email'];
			$data ['depid'] = $_POST ['depid'];
			$data ['jobid'] = $_POST ['jobid'];
			$data ['phone'] = $_POST ['phone'];
			$data ['fax'] = $_POST ['fax'];
			$data ['address'] = $_POST ['address'];
			$data ['accessiontime'] = $_POST ['accessiontime'];
			$data ['education'] = $_POST ['education'];
			$data ['borrowamount'] = $_POST ['borrowamount'];
			$data ['status'] = $_POST ['status'];
			$data ['photo'] = $imgurl;
			
			$result = $User->where ( 'id=' . $id )->save ( $data );
			if ($result) {
				$this->ajaxReturn ( $result, "信息添加成功", '200' );
			} else {
				$this->ajaxReturn ( '0', "信息无更改", '200' );
			}
		} else {
			$this->error ( $User->getError () );
		}
	}
	}
	// 删除
	public function delete() {
		if (! empty ( $_GET ['userid'] )) {
			echo $_GET ['userid'];
			$User = D ( "User" );
			$result = $User->delete ( $_GET ['userid'] );
			$this->redirect ( "index" );
		} else {
			$this->error ( '没有获得ID值！' );
		}
	}
	public function remove() {
		$id = $_REQUEST ['ids'];
		$User = M ( 'User' );
		$result = $User->delete ( $id );
		if ($result) {
			$this->ajaxReturn ( $result, '用户删除成功！', 200 );
		} else {
			$this->error ( '删除失败' );
		}
	}
	// 修改
	public function edit($id = 0) {
		$User = M ( 'User' );
		$this->vo = $User->find ( $id );
		$this->display ();
	}
	// 检查帐号
	/**
	 */
	public function checkuUsername() {
		$User = M ( "User" );
		// 检测用户名是否冲突
		$name = $_REQUEST ['username'];
		$result = $User->getByAccount ( $name );
		if ($result) {
			$this->ajaxReturn ( '0', '该用户名已经存在!', '300' );
		} else {
			$this->ajaxReturn ( '1', '该用户名可以使用！', '200' );
		}
	}
	
	// 重置密码
	public function resetPwd() {
		if (md5 ( $_POST ['verify'] ) != $_SESSION ['verify']) {
			$this->ajaxReturn ( '0', '验证码错误！', '300' );
		}
		$id = $_POST ['id'];
		$password = $_POST ['password'];
		if ('' == trim ( $password )) {
			$this->error ( '密码不能为空！' );
		}
		$User = M ( 'User' );
		$User->password = md5 ( $password );
		$User->id = $id;
		$result = $User->save ();
		if (false !== $result) {
			$this->ajaxReturn ( '1', "密码修改为$password", '200' );
		} else {
			$this->ajaxReturn ( '0', '重置密码失败！', '300' );
		}
	}
}